package com.example.canteen.web.servlet;

import com.example.canteen.domain.Canteen;
import com.example.canteen.domain.ResultInfo;
import com.example.canteen.service.CanteenService;
import com.example.canteen.service.impl.CanteenSerViceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
@WebServlet(name = "CanteenServlet", value = "/Canteen/*")
public class CanteenServlet extends BaseServlet{
    CanteenService canteenService = new CanteenSerViceImpl();
    public void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取数据
        Map<String, String[]> parameterMap = request.getParameterMap();
        //封装对象
        Canteen canteen = new Canteen();
        try {
            BeanUtils.populate(canteen,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        boolean flag = canteenService.save(canteen);
        ResultInfo info = new ResultInfo();
        //响应结果
        if(flag){
            info.setFlag(true);
        }
        else{
            info.setFlag(false);
            info.setErrorMsg("添加失败");
        }

        writeValue(info,response);
    }
    //查找一个
    public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Canteen canteen = canteenService.findOne(Integer.valueOf(id));
        writeValue(canteen,response);
    }
    //删除
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        boolean b = canteenService.delete(Integer.valueOf(id));

        ResultInfo info = new ResultInfo();
        if (!b){
            info.setFlag(false);
            info.setErrorMsg("删除失败");
        }
        else{
            info.setFlag(true);
        }
        writeValue(info,response);
    }
    //查找所有
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<Canteen> all = canteenService.findAll();

        writeValue(all,response);
    }
    //更新
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        //封装对象
        Canteen canteen = new Canteen();
        try {
            BeanUtils.populate(canteen,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean b = canteenService.update(canteen);
        ResultInfo info = new ResultInfo();
        if (!b){
            info.setFlag(false);
            info.setErrorMsg("更新失败");
        }
        else{
            info.setFlag(true);
        }

        writeValue(info,response);
    }
}
